<?php

namespace app\api\model;

use think\Model;

class TradeLog extends Model
{
    protected $name = 'user_order_buy_log';

    public function addTransfer1($buy_order_id, $user_id, $sell_user_id, $user_mobile, $order_type, $deal_ticket, $deal_money, $remark)
    {
        $aa = TradeLog::create([
            'buy_order_id' => $buy_order_id,
            'buy_user_id' => $sell_user_id,//id
            'sell_user_id' => $user_id,//id
            'user_mobile' => $user_mobile,//手机号
            'order_type' => $order_type,
            'deal_ticket' => $deal_ticket,//交易积分
            'buy_deal_status' => 2,//买单交易状态
            'sell_deal_status' => 1,//卖单交易状态
            'order_number' => mt_rand(1000000000, 9999999999) . strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 9)),
            'deal_money' => $deal_money,
            'effective_start_time' => date('Y-m-d H:i:s'),
            'effective_end_time' => date('Y-m-d H:i:s', time() + 1800),
            'create_by' => $user_mobile,//创建人'
            'create_time' => date('Y-m-d H:i:s'),
            'remark' => $remark,//备注
        ]);
        return $aa->getLastInsID();
    }

    public function addTransfer2($sell_order_id, $user_id, $sell_user_id, $image, $user_mobile, $order_type, $deal_ticket, $deal_money, $remark)
    {
//        halt(empty($image) ? 2 : 1);
        $aa = TradeLog::create([
            'sell_order_id' => $sell_order_id,
            'buy_user_id' => $user_id,//id
            'sell_user_id' => $sell_user_id,//id
            'user_mobile' => $user_mobile,//手机号
            'order_type' => $order_type,
            'deal_ticket' => $deal_ticket,//交易积分
            'deal_money' => $deal_money, //交易金额
            'pay_voucher' => $image,
            'buy_deal_status' => empty($image) ? 2 : 1,//买单交易状态(2待付款1待审核)
            'sell_deal_status' => 1,//卖单交易状态（待放行）
            'order_number' => mt_rand(1000000000, 9999999999) . strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 9)),
            'effective_start_time' => date('Y-m-d H:i:s'),
            'effective_end_time' => date('Y-m-d H:i:s', time() + 1800),
            'create_by' => $user_mobile,//创建人'
            'create_time' => date('Y-m-d H:i:s'),
            'remark' => $remark,//备注
        ]);
        return $aa->getLastInsID();
    }

    //更新交易截图
    public function updateImg1($id, $img)
    {
        TradeLog::where(['buy_log_id' => $id])->update(['pay_voucher' => $img]);
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->field('id,avatar,nickname');
    }
}